//
//  GoldenVPNOrderListVC.m
//  QLadder
//
//  Created by xiongguobing on 2019/8/19.
//  Copyright © 2019 qding. All rights reserved.
//

#import "GoldenVPNOrderListVC.h"
#import "OrderListModel.h"
#import "GoldenVPNOrderCell.h"
#import "GoldenVPNPayModelHandle.h"
#import "GoldenVPNPaymentVC.h"

static NSString * serviceListVCToPaymentVCSegue = @"serviceListVCToPaymentVCSegue";

@interface GoldenVPNOrderListVC ()

@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, strong) NSString * orderNo;

@end

@implementation GoldenVPNOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [kNotificationCenter addObserver:self selector:@selector(didPaySuccessNotification) name:kPaymentDidPaySuccess object:nil];
    // Do any additional setup after loading the view.
}

- (void)loadData
{
    NSString * userName = [kUserDefaults objectForKey:kUserAccount];
    if (userName) {
        [kNetworkUtil postWithModuleURLStr:userOrders parameters:@{@"loginName" : userName} success:^(id response) {
            if ([[response objectForKey:@"code"] integerValue] == 200) {
                NSArray * arr = [OrderListModel mj_objectArrayWithKeyValuesArray:response[@"orders"]];
                self.dataArray = arr;
                [self.tableView reloadData];
            }
        } failure:nil withHud:YES];
    }
}

- (void)didPaySuccessNotification
{
    [self loadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoldenVPNOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GoldenVPNOrderCell"];
    OrderListModel * model = self.dataArray[indexPath.row];
    cell.model = model;
    kWeakSelf(self);
    [cell handlePayButtonClickedBlock:^{
        weakself.orderNo = model.orderNo;
        [weakself performSegueWithIdentifier:serviceListVCToPaymentVCSegue sender:weakself];
    }];
    return cell;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:serviceListVCToPaymentVCSegue]) {
        GoldenVPNPaymentVC * payVC = segue.destinationViewController;
        payVC.orderNo = self.orderNo;
    }
}


@end
